package stud.aistar.controller;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import stud.aistar.entity.User;
import stud.aistar.entity.Userinfo;
import stud.aistar.service.IUserInfoService;
import stud.aistar.util.JSONChange;
import stud.aistar.util.QiniuCloudUtil;
import stud.aistar.vo.AgeVo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Auther: GIT:liming  -  jybdd
 * @Description - 本类用于: 个人中心
 * @Date: 2019/9/30 17:43
 */
@Controller
@SuppressWarnings("all")
public class FileController {

    @Autowired
    private IUserInfoService userInfoService;

    @RequestMapping ("/testss")
    public String testss(){
        return "personal/ocr";
    }

    @RequestMapping(value = "/idCard",method = RequestMethod.POST)
    @ResponseBody
    public Map idCard(@RequestParam("file_idcard") MultipartFile[] fileArr,MultipartFile myfile) throws Exception{
        MultipartFile mf1 = fileArr[0];
        MultipartFile mf2 = fileArr[1];
        byte[] by1 = mf1.getBytes();
        byte[] by2 = mf2.getBytes();
        AipOcr client1 = new AipOcr("17473451","cFpt97instrAtyhAzhpoLDrD","O1GP7V7dHikAcplf46rEnM3w0y4bmnsW");
        JSONObject jo_fr = client1.idcard(by1,"front",new HashMap<String, String>());    //正面
//        System.out.println("正面：" + jo_fr);
        AipOcr client2 = new AipOcr("17473451","cFpt97instrAtyhAzhpoLDrD","O1GP7V7dHikAcplf46rEnM3w0y4bmnsW");
        JSONObject jo_ba = client2.idcard(by2,"back",new HashMap<String, String>());     //背面
//        System.out.println("背面：" + jo_ba);
        Map map = new HashMap<String,String>();
        //身份识别
        map.put("front",JSONChange.json2map(jo_fr.toString()));
        map.put("back",JSONChange.json2map(jo_ba.toString()));
        return map;
    }

    @RequestMapping(value = "/ocr",method = RequestMethod.POST)
    @ResponseBody
    public Map ocr(MultipartFile myfile) throws Exception{
        //接收图像二进制数据
        byte[] buf = myfile.getBytes();
        //初始化百度接口
        AipOcr client = new AipOcr("17473451","cFpt97instrAtyhAzhpoLDrD","O1GP7V7dHikAcplf46rEnM3w0y4bmnsW");
        //首选参数
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");    //中英语言
        JSONObject res = client.basicGeneral(buf, options);
        Map map = JSONChange.json2map(res.toString());
        return map;
    }
}
